<template>
  <div class="login-container">
    <el-form
      class="login-form"
      ref="loginFormRef"
      :model="user"
      :rules="loginFormRules"
    >
      <div class="login-head"></div>
      <el-form-item prop="mobile">
        <el-input v-model="user.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="user.code" placeholder="请输入验证码"></el-input>
      </el-form-item>
      <el-form-item prop="agree">
        <el-checkbox v-model="user.agree"
          >我已阅读并同意用户协议和隐私条款</el-checkbox
        >
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          class="login-btn"
          @click="onLogin"
          :loading="loginLoading"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  name: 'LoginIndex',
  data () {
    return {
      user: {
        mobile: '13911111111',
        code: '246810',
        agree: false
      },
      loginLoading: false, // 登录按钮加载状态，网络慢时防止用户多次点击
      loginFormRules: {
        mobile: [
          { required: true, message: '手机号码不得为空', trigger: 'change' },
          {
            pattern: /^1[3|5|7|8|9]\d{9}$/,
            message: '请输入正确的手机号码',
            trigger: 'change'
          }
        ],
        code: [
          { required: true, message: '验证码不得为空', trigger: 'change' },
          { pattern: /^\d{6}$/, message: '请输入正确的验证码' }
        ],
        agree: [
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('请同意用户协议'))
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {},
  methods: {
    onLogin () {
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) {
          return false
        }
        this.userLogin()
      })
    },
    userLogin () {
      this.loginLoading = true
      login(this.user).then((res) => { // 请求
        this.$message.success('登陆成功')
        this.loginLoading = false
        window.localStorage.setItem('user', JSON.stringify(res.data.data))
        this.$router.push('/')
      })
        .catch((err) => {
          console.log(err)
          this.$message.error('登录失败，手机号码或验证码错误！')
          this.loginLoading = false
        })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url('./login_bg.jpg') no-repeat;
  background-size: cover;
  .login-head {
    width: 200px;
    height: 57px;
    margin: 0 auto;
    background: url('./logo_index.png') no-repeat;
    background-size: contain;
  }
  .login-form {
    background-color: #fff;
    padding: 20px 50px 17px;
    min-width: 300px;
    .login-btn {
      width: 100%;
    }
  }
}
</style>
